package com.ayou.eggguard.farm.service.impl;

import com.ayou.common.utils.PageBean;
import com.ayou.eggguard.farm.mapper.RelatedDrugMapper;
import com.ayou.eggguard.farm.mapper.TreatMethodMapper;
import com.ayou.eggguard.farm.pojo.RelatedDrug;
import com.ayou.eggguard.farm.pojo.TreatMethod;
import com.ayou.eggguard.farm.pojo.TreatmentPlan;
import com.ayou.eggguard.farm.service.TreatMethodService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TreatMethodServiceImpl implements TreatMethodService {
    @Autowired
    private TreatMethodMapper treatMethodMapper;

    @Autowired
    private RelatedDrugMapper relatedDrugMapper;

    @Override
    public PageBean<TreatMethod> getMethods(int pageNum, int pageSize, String methodName) {
        PageBean<TreatMethod> pb=new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<TreatMethod> list= treatMethodMapper.getMethods(methodName);
        Page<TreatMethod> p= (Page<TreatMethod>) list;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void delete(Integer id) {
        treatMethodMapper.delete(id);
    }

    @Override
    public PageBean<TreatmentPlan> getMethodsList(int pageNum, int pageSize, String methodName) {
        PageBean<TreatmentPlan> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize, true);
        List<String> methodNameList = treatMethodMapper.getMethodNameList(methodName);
        List<TreatmentPlan> plans = new ArrayList<>();
        for (String method : methodNameList) {
            List<RelatedDrug> drugList;
            drugList = treatMethodMapper.getDrugsByMethodName(method);
            TreatmentPlan plan = new TreatmentPlan();
            plan.setMethodName(method);
            String dec = treatMethodMapper.getMethodDescription(method);
            plan.setMethodDescription(dec);
            plan.setDrugs(drugList);
            plans.add(plan);
        }
        long total=treatMethodMapper.getTotalMethod(methodName);
        PageInfo<TreatmentPlan> pageInfo = new PageInfo<>(plans);
        pb.setItems(pageInfo.getList());
        pb.setTotal(total);
        return pb;
    }

    @Override
    public void deleteMethod(String methodName) {
        treatMethodMapper.deleteMethod(methodName);
    }

    @Override
    public void addTreatmentPlan(String methodName, String methodDescription, List<RelatedDrug> drugList) {
        treatMethodMapper.add(methodName,methodDescription);
        for(RelatedDrug drug:drugList){
            relatedDrugMapper.add(drug);
        }
    }

    @Override
    public List<String> getMethodName() {
        return treatMethodMapper.getMethodName();
    }


}
